<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" label-width="120px" ref="HelpForm">
      <el-form-item label="热搜词" prop="name" required>
        <el-input v-model="form.name" style="width:300px" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" type="number" style="width:202px" />
      </el-form-item>

      <el-form-item label="推荐" prop="is_recommend" required>
        <el-switch v-model="form.is_recommend" active-text="是" inactive-text="否">
        </el-switch>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('HelpForm')" size="small">提 交</el-button>
        <el-button @click="onCancel()" size="small">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createHotSearch, updateHotSearch, detailHotSearch } from '@/api/config/hotSearch'
export default {
  components: {},
  data() {
    return {
      form: {
        name: '',
        sort: 99,
        is_recommend: true,
      },
      rules: {
        name: [{ required: true, message: '热搜词不能为空', trigger: 'blur' }],
      }
    }
  },
  beforeMount() {
    let id = this.$route.params.id || 0;
    if (id > 0) {
      detailHotSearch(id).then(response => {
        this.form = response.data
        this.form.is_recommend = response.data.is_recommend ? true : false
      })
    }
  },
  methods: {
    onSubmit(formName) {

      this.form.is_recommend = this.form.is_recommend ? 1 : 0

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.checkSpec()
          let id = this.$route.params.id || 0;
          if (id > 0) {
            updateHotSearch(this.form).then(response => {
              if (response.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(() => {
                  this.$router.push({ name: 'hotSearch' });
                }, 200)
              }
            })
          } else {
            createHotSearch(this.form).then(response => {
              if (response.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                setTimeout(() => {
                  this.$router.push({ name: 'hotSearch' });
                }, 200)
              }
            })
          }
        }
      })
    },
    checkSpec() {
      let _spec_name = this.form.name
      if (!_spec_name || _spec_name.length < 1) {
        this.$message({
          message: '热搜词不能为空',
          type: 'warning'
        })
        return false;
      }

      return true;
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.app-container {
  background: #fff;
  margin-top: 18px;
  height: 100%;
  min-height: calc(100vh - 68px);
  font-size: 12px
}

.app-container * {
  font-size: 12px;
}
</style>
